package graduate.wangyaqi.QuestionDetail;

import graduate.wangyaqi.Question.Question;
import graduate.wangyaqi.Question.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.Optional;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(path="/QuestionDetail")
public class QuestionDetailController {
    @Autowired
    private QuestionDetailService questionDetailService;
    private QuestionService questionService;

    @GetMapping(path="/findById")
    public Optional<QuestionDetail> findById(Long aLong){
        return questionDetailService.findById(aLong);
    }

    @GetMapping(path="/getAll")
    public Iterable<QuestionDetail> getAll(){
        return questionDetailService.getAll();
    }

    @PostMapping(path="/insert")
    public boolean InsertQuestion(@RequestBody  QuestionDetail item){
        long questionId = item.getQuestionId();
        if(questionId==0){
            return false;
        }
        item.setAnswerTime(new Date());
        questionDetailService.save(item);
        return true;
    }

    @GetMapping(path="/findByQuestionId")
    public Iterable<QuestionDetail> InsertQuestion(Long id){
        return questionDetailService.findQuestionDetailByQuestionId(id);
    }
}
